package com.ym.jiujiangeyou;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.ym.jiujiangeyou.adapter.CollectListAdapter;
import com.ym.jiujiangeyou.app.AppManager;
import com.ym.jiujiangeyou.entity.Guide;
import com.ym.jiujiangeyou.entity.Scenery;
import com.ym.jiujiangeyou.ui.DialogView;
import com.ym.jiujiangeyou.util.Constants;
import com.ym.jiujiangeyou.database.SceneryService;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

/**
 * @author 叶铭
 * @email yeming_1001@163.com
 * @version V1.0
 */
@SuppressWarnings("unused")
public class CollectList extends Activity {

	private CollectListAdapter adapter;
	private List<Scenery> scenerys, Slist;
	private List<Guide> guides, Glist;
	private TextView title_text;
	private ListView listView;
	SceneryService service;
	Scenery scenery;
	Guide guide;
	private int action;
	Dialog dialog;
	Toast toast;
	ImageView collect_del_img ;
	boolean isDel = false ;   //是否删除标志

	@SuppressLint("HandlerLeak")
	Handler handler = new Handler() {
		@SuppressWarnings("unchecked")
		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			Map<String, Object> map;
			switch (msg.what) {
			case Constants.LISTVIEW_ACTION_GET_SCENERY:
				map = (Map<String, Object>) msg.obj;
				scenerys = (List<Scenery>) map.get("scenery");
				guides = (List<Guide>) map.get("guide");
				if(scenerys.size() < 1 || guides.size() < 1){
					Toast.makeText(CollectList.this, "没有收藏信息", Toast.LENGTH_SHORT).show() ;
				}
				Slist.addAll(scenerys);
				Glist.addAll(guides);
				adapter.setSceneryData(Slist);
				listView.setAdapter(adapter);
				dialog.dismiss();
				break;
			case Constants.LISTVIEW_ACTION_GET_ERROR:
				dialog.dismiss();
				toast = DialogView.popToast(CollectList.this,
						R.string.load_error);
				toast.show();
				break;
			case Constants.LIST_COLLECT_IS_DEL:
				isDel = true ;
				adapter.setIsDel(isDel) ;
				adapter.setSceneryData(Slist);
				listView.setAdapter(adapter);
				break ;
			case Constants.LIST_COLLECT_NO_DEL:
				isDel = false ;
				adapter.setIsDel(isDel) ;
				adapter.setSceneryData(Slist);
				listView.setAdapter(adapter);
				break ;
			case Constants.LISTVIEW_ACTION_GET_HOTEL_AGIAN:
				Slist.clear() ;
				Glist.clear() ;
				map = (Map<String, Object>) msg.obj;
				scenerys = (List<Scenery>) map.get("scenery");
				guides = (List<Guide>) map.get("guide");
				if(scenerys.size() < 1 || guides.size() < 1){
					Toast.makeText(CollectList.this, "没有收藏信息", Toast.LENGTH_SHORT).show() ;
				}
				Slist.addAll(scenerys);
				Glist.addAll(guides);
				adapter.setSceneryData(Slist);
				listView.setAdapter(adapter);
				dialog.dismiss();
				break ;
			case Constants.SCENERY_COLLECT_NOT:
				dialog.dismiss();
				Toast.makeText(CollectList.this, "你还没有收藏酒店信息", Toast.LENGTH_SHORT).show() ;
				break ;
			default:
				break;
			}
			adapter.notifyDataSetChanged();
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		
		// 添加Activity到堆栈
		AppManager.getAppManager().addActivity(this);
		
		super.setContentView(R.layout.collect_list);

		Intent intent = getIntent();
		action = intent.getIntExtra("action", 0);

		init();

		if (Slist.isEmpty()) {
			loadData(handler, action);
		}
	}

	/**
	 * @retur void
	 * @Description: 初始化
	 */
	public void init() {
		title_text = (TextView) findViewById(R.id.title_text);
		listView = (ListView) findViewById(R.id.collectlist);
		collect_del_img = (ImageView) findViewById(R.id.collect_del_img) ;

		dialog = DialogView.popDialog(this, R.string.isloading);
		scenerys = new ArrayList<Scenery>();
		Slist = new ArrayList<Scenery>();
		Glist = new ArrayList<Guide>();
		guides = new ArrayList<Guide>();
		service = new SceneryService(CollectList.this);
		adapter = new CollectListAdapter(this,Constants.LISTVIEW_ACTION_GET_SCENERY);
		title_text.setText(R.string.title_text_collectlist);

		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1,
					int position, long arg3) {
				// TODO Auto-generated method stub
				if(!isDel){
					Intent intent = new Intent(CollectList.this,
							SceneryDetails.class);
					intent.putExtra("scenery", (Scenery) Slist.get(position));
					intent.putExtra("guide", (Guide) Glist.get(position));
					startActivity(intent);
				}else{
					Scenery s =  (Scenery) Slist.get(position) ;
					SceneryService sceneryService = new SceneryService(CollectList.this) ;
					sceneryService.delByName(s.getName()) ;
					loadData(handler,Constants.LISTVIEW_ACTION_GET_HOTEL_AGIAN ) ;
				}
				
			}
		});
		
		collect_del_img.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Toast.makeText(getApplicationContext(), "del", Toast.LENGTH_SHORT).show() ;
				Message msg_del = new Message() ;
				if(!isDel){
					msg_del.what = Constants.LIST_COLLECT_IS_DEL ;
				}else{
					msg_del.what = Constants.LIST_COLLECT_NO_DEL ;
					
				}
				handler.sendMessage(msg_del) ;
			}
			
			
		}) ;
	}

	/**
	 * @param handler
	 * @param action
	 * @retur void
	 * @Description: 加载数据库数据
	 */
	void loadData(final Handler handler, final int action) {
		dialog.show();
		new Thread(new Runnable() {
			Message message = new Message();
			Map<String, Object> map;

			@Override
			public void run() {
				// TODO Auto-generated method stub
				switch (action) {
				case Constants.LISTVIEW_ACTION_GET_SCENERY:
					
					try {
						map = service.query();
						if (!map.isEmpty()) {
							// scenerys = (List<Scenery>) map.get("scenery") ;
							// guides = (List<Guide>) map.get("guide") ;
							message.what = Constants.LISTVIEW_ACTION_GET_SCENERY;
							message.obj = map;

						} else {
							message.what = Constants.SCENERY_COLLECT_NOT;
						}
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
						message.what = Constants.LISTVIEW_ACTION_GET_ERROR;
					}
					
					break;
				case Constants.LISTVIEW_ACTION_GET_HOTEL_AGIAN:
					try {
						map = service.query();
						if (!map.isEmpty()) {
							// scenerys = (List<Scenery>) map.get("scenery") ;
							// guides = (List<Guide>) map.get("guide") ;
							message.what = Constants.LISTVIEW_ACTION_GET_HOTEL_AGIAN;
							message.obj = map;

						} else {
							message.what = Constants.SCENERY_COLLECT_NOT;
						}
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
						message.what = Constants.LISTVIEW_ACTION_GET_ERROR;
					}
					break ;
				default:
					break;
				}
				handler.sendMessage(message);
			}
		}).start();
	}

}
